﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;

namespace YArchitech.HVAC
{
	public class SystemManageHelper
	{
		public systemTypeStruct[] SaveArray(string systemNmae, List<systemTypeStruct> structList)
		{
			IEnumerable<systemTypeStruct> source = from c in structList
			where c.SystemClass == systemNmae
			select c;
			int num = source.Count<systemTypeStruct>();
			systemTypeStruct[] array = new systemTypeStruct[0];
			array = new systemTypeStruct[num];
			if (array.Count<systemTypeStruct>() > 0)
			{
				for (int i = 0; i < num; i++)
				{
					array[i] = new systemTypeStruct();
				}
				for (int j = 0; j < num; j++)
				{
					array[j].PipingSystemType = source.ToArray<systemTypeStruct>().ElementAt(j).PipingSystemType;
					array[j].PipingSystemTypeAbb = source.ToArray<systemTypeStruct>().ElementAt(j).PipingSystemTypeAbb;
					array[j].PipeSystemType = source.ToArray<systemTypeStruct>().ElementAt(j).PipeSystemType;
					array[j].Color = source.ToArray<systemTypeStruct>().ElementAt(j).Color;
					array[j].LinePattern = source.ToArray<systemTypeStruct>().ElementAt(j).LinePattern;
					array[j].LineWeight = source.ToArray<systemTypeStruct>().ElementAt(j).LineWeight;
					array[j].SystemClass = source.ToArray<systemTypeStruct>().ElementAt(j).SystemClass;
				}
			}
			return array;
		}

		public void SaveBigList(bigStruct bs, List<systemTypeStruct> structList)
		{
			List<systemTypeStruct> list = new List<systemTypeStruct>();
			List<systemTypeStruct> list2 = new List<systemTypeStruct>();
			systemTypeStruct[] array = this.SaveArray("空调风系统", structList);
			if (array != null)
			{
				list.AddRange(array);
			}
			array = this.SaveArray("空调水系统", structList);
			if (array != null)
			{
				list2.AddRange(array);
			}
			array = this.SaveArray("给排水系统", structList);
			if (array != null)
			{
				list2.AddRange(array);
			}
			array = this.SaveArray("采暖系统", structList);
			if (array != null)
			{
				list2.AddRange(array);
			}
			array = this.SaveArray("消防系统", structList);
			if (array != null)
			{
				list2.AddRange(array);
			}
			array = this.SaveArray("其他", structList);
			if (array != null)
			{
				list2.AddRange(array);
			}
			array = this.SaveArray("风管系统", structList);
			if (array != null)
			{
				list.AddRange(array);
			}
			array = this.SaveArray("管道系统", structList);
			if (array != null)
			{
				list2.AddRange(array);
			}
			bs.DuctSystemStruct = list.ToArray();
			bs.PipeSystemStruct = list2.ToArray();
		}

		public void SaveBigList(bigStruct bs)
		{
			List<systemTypeStruct> list = new List<systemTypeStruct>();
			List<systemTypeStruct> list2 = new List<systemTypeStruct>();
			systemTypeStruct[] array = this.SaveArray("空调风系统", SystemTypeManageForm.structList);
			if (array != null)
			{
				list.AddRange(array);
			}
			array = this.SaveArray("空调水系统", SystemTypeManageForm.structList);
			if (array != null)
			{
				list2.AddRange(array);
			}
			array = this.SaveArray("给排水系统", SystemTypeManageForm.structList);
			if (array != null)
			{
				list2.AddRange(array);
			}
			array = this.SaveArray("采暖系统", SystemTypeManageForm.structList);
			if (array != null)
			{
				list2.AddRange(array);
			}
			array = this.SaveArray("消防系统", SystemTypeManageForm.structList);
			if (array != null)
			{
				list2.AddRange(array);
			}
			array = this.SaveArray("其他", SystemTypeManageForm.structList);
			if (array != null)
			{
				list2.AddRange(array);
			}
			array = this.SaveArray("风管系统", SystemTypeManageForm.structList);
			if (array != null)
			{
				list.AddRange(array);
			}
			array = this.SaveArray("管道系统", SystemTypeManageForm.structList);
			if (array != null)
			{
				list2.AddRange(array);
			}
			bs.DuctSystemStruct = list.ToArray();
			bs.PipeSystemStruct = list2.ToArray();
		}

		public static string getNewRGB(byte R)
		{
			string result = "";
			if (R <= 9)
			{
				result = "00" + R.ToString();
			}
			else if (R <= 99 && R >= 10)
			{
				result = "0" + R.ToString();
			}
			if (R >= 100)
			{
				result = R.ToString();
			}
			return result;
		}

		public static System.Drawing.Color GetColorAboutInt(int color)
		{
			int red = color & 255;
			int green = (color & 65280) >> 8;
			int blue = (color & 16711680) >> 16;
			return System.Drawing.Color.FromArgb(red, green, blue);
		}

		public static int GetIntAboutColor(System.Drawing.Color color)
		{
			return (int)color.B << 16 | (int)((ushort)((int)color.G << 8 | (int)color.R));
		}

		public static string getColorRGBString(System.Drawing.Color colorCell)
		{
			string newRGB = SystemManageHelper.getNewRGB(colorCell.R);
			string newRGB2 = SystemManageHelper.getNewRGB(colorCell.G);
			string newRGB3 = SystemManageHelper.getNewRGB(colorCell.B);
			return string.Concat(new string[]
			{
				"█RGB ",
				newRGB.ToString(),
				" ",
				newRGB2.ToString(),
				" ",
				newRGB3.ToString()
			});
		}

        public static Autodesk.Revit.DB.Color getRevirColor(string colorRGB)
		{
			return new Autodesk.Revit.DB.Color((byte)int.Parse(colorRGB.Split(new char[]
			{
				' '
			}).ElementAt(1)), (byte)int.Parse(colorRGB.Split(new char[]
			{
				' '
			}).ElementAt(2)), (byte)int.Parse(colorRGB.Split(new char[]
			{
				' '
			}).ElementAt(3)));
		}

		public static LinePatternElement CreateLinePatternElement(string patternName, Autodesk.Revit.DB.Document doc)
		{
			List<LinePatternSegment> list = new List<LinePatternSegment>();
			list.Add(new LinePatternSegment((LinePatternSegmentType)2, 0.0));
			list.Add(new LinePatternSegment((LinePatternSegmentType)1, 0.02));
			list.Add(new LinePatternSegment((LinePatternSegmentType)0, 0.03));
			list.Add(new LinePatternSegment((LinePatternSegmentType)1, 0.02));
			LinePattern linePattern = new LinePattern(patternName);
			linePattern.SetSegments(list);
			return LinePatternElement.Create(doc, linePattern);
		}

		public static List<LinePatternElement> Get_LinePatternElement(Autodesk.Revit.DB.Document document)
		{
			IEnumerable<Element> enumerable = new FilteredElementCollector(document).OfClass(typeof(LinePatternElement)).ToElements();
			List<LinePatternElement> list = new List<LinePatternElement>();
			foreach (Element element in enumerable)
			{
				LinePatternElement item = element as LinePatternElement;
				list.Add(item);
			}
			return list;
		}

		public static System.Drawing.Color getXMLDataColor(string rgb)
		{
			System.Drawing.Color result = default(System.Drawing.Color);
			if (rgb != "默认")
			{
				result = System.Drawing.Color.FromArgb(int.Parse(rgb.Split(new char[]
				{
					' '
				}).ElementAt(1)), int.Parse(rgb.Split(new char[]
				{
					' '
				}).ElementAt(2)), int.Parse(rgb.Split(new char[]
				{
					' '
				}).ElementAt(3)));
			}
			else
			{
				result = System.Drawing.Color.FromArgb(128, 128, 128);
			}
			return result;
		}

		public static string getRGB(PipingSystemType pst)
		{
			string result;
			if (pst.LineColor.IsValid)
			{
				string newRGB = SystemManageHelper.getNewRGB(pst.LineColor.Red);
				string newRGB2 = SystemManageHelper.getNewRGB(pst.LineColor.Green);
				string newRGB3 = SystemManageHelper.getNewRGB(pst.LineColor.Blue);
				result = string.Concat(new string[]
				{
					"█RGB ",
					newRGB.ToString(),
					" ",
					newRGB2.ToString(),
					" ",
					newRGB3.ToString()
				});
			}
			else
			{
				result = "█RGB 128 128 128";
			}
			return result;
		}

		public static string getRGB(MechanicalSystemType mst)
		{
			string result;
			if (mst.LineColor.IsValid)
			{
				string newRGB = SystemManageHelper.getNewRGB(mst.LineColor.Red);
				string newRGB2 = SystemManageHelper.getNewRGB(mst.LineColor.Green);
				string newRGB3 = SystemManageHelper.getNewRGB(mst.LineColor.Blue);
				result = string.Concat(new string[]
				{
					"█RGB ",
					newRGB.ToString(),
					" ",
					newRGB2.ToString(),
					" ",
					newRGB3.ToString()
				});
			}
			else
			{
				result = "█RGB 128 128 128";
			}
			return result;
		}

		public static string getLinePattern(PipingSystemType pst, Autodesk.Revit.DB.Document doc)
		{
			string result = null;
			if (pst.LinePatternId.IntegerValue != -1)
			{
				try
				{
					return LinePatternElement.GetLinePattern(doc, pst.LinePatternId).Name;
				}
				catch
				{
					return "默认";
				}
			}
			result = "默认";
			return result;
		}

		public static string getLinePattern(MechanicalSystemType pst, Autodesk.Revit.DB.Document doc)
		{
			string result = null;
			if (pst.LinePatternId.IntegerValue != -1)
			{
				try
				{
					return LinePatternElement.GetLinePattern(doc, pst.LinePatternId).Name;
				}
				catch
				{
					return "默认";
				}
			}
			result = "默认";
			return result;
		}

		public static string getLineWeight(MechanicalSystemType pst)
		{
			string result;
			if (pst.LineWeight == 0 || pst.LineWeight == 1 || pst.LineWeight == 2 || pst.LineWeight == 3 || pst.LineWeight == 4 || pst.LineWeight == 5 || pst.LineWeight == 6 || pst.LineWeight == 7 || pst.LineWeight == 8 || pst.LineWeight == 9 || pst.LineWeight == 10 || pst.LineWeight == 11 || pst.LineWeight == 12 || pst.LineWeight == 13 || pst.LineWeight == 14 || pst.LineWeight == 15 || pst.LineWeight == 16)
			{
				result = pst.LineWeight.ToString();
			}
			else
			{
				result = "默认";
			}
			return result;
		}

		public static string getLineWeight(PipingSystemType pst)
		{
			string result;
			if (pst.LineWeight == 0 || pst.LineWeight == 1 || pst.LineWeight == 2 || pst.LineWeight == 3 || pst.LineWeight == 4 || pst.LineWeight == 5 || pst.LineWeight == 6 || pst.LineWeight == 7 || pst.LineWeight == 8 || pst.LineWeight == 9 || pst.LineWeight == 10 || pst.LineWeight == 11 || pst.LineWeight == 12 || pst.LineWeight == 13 || pst.LineWeight == 14 || pst.LineWeight == 15 || pst.LineWeight == 16)
			{
				result = pst.LineWeight.ToString();
			}
			else
			{
				result = "默认";
			}
			return result;
		}
	}
}
